<?php

// Namespace

namespace Loop;


/**
 * CJob is a class that can be used to schedule jobs
 *
 * @package		Loop
 * @category	Model
 */

class CJob extends \Loop\CObject
{	

	/**
	 *
	 */
	
	public function api_hold()
	{
		// Get API arguments
		
		$objectId = fv('objectId', true);
		$objectReference = fv('objectReference', true);


		// Load the object
		
		$this->loadSecure($objectId, $objectReference);


		// Prepare result before Hold it

		$result = array
		(
			'urlOfRedirection' => $this->getUrlOfPage(ACTION_VIEW)
		);


		// Call the method

		$this->hold();


		return $result;
	}


	/**
	 *
	 */
	
	public function api_release()
	{
		// Get API arguments
		
		$objectId = fv('objectId', true);
		$objectReference = fv('objectReference', true);
		
		
		// Load the object
		
		$this->loadSecure($objectId, $objectReference);


		// Prepare result before Release it

		$result = array
		(
			'urlOfRedirection' => $this->getUrlOfPage(ACTION_VIEW)
		);


		// Call the method

		$this->release();


		return $result;	
	}


	/**
	 *
	 */

	public function api_run()
	{
		// Get API arguments
		
		$objectId = fv('objectId', true);
		$objectReference = fv('objectReference', true);
		
		
		// Load the object
		
		$this->loadSecure($objectId, $objectReference);


		// Prepare result before Run it

		$result = array
		(
			'urlOfRedirection' => $this->getUrlOfPage(ACTION_VIEW)
		);


		// Call the method

		$this->run();


		return $result;		
	}



	/**
	 *
	 */
	
	protected function createProperties()
	{
		$this->createProperty
		(
			'apiName',
			array 
			(
				'isRequired' => true,
				'type' => TYPE_STRING_LONG
			)
		);

		$this->createProperty
		(
			'arguments',
			array 
			(
				'type' => TYPE_STRING_LONG
			)
		);

		$this->createProperty
		(
			'category',
			array 
			(
				'type' => TYPE_STRING_MEDIUM
			)
		);		

		$this->createProperty
		(
			'className',
			array 
			(
				'isRequired' => true,
				'type' => TYPE_STRING_LONG
			)
		);

 		$this->createProperty
		(
			'days',
			array
			(
				'type' => TYPE_STRING_LONG
			)
		);

 		$this->createProperty
		(
			'daysOfWeek',
			array
			(
				'type' => TYPE_STRING_LONG
			)
		);

		$this->createProperty
		(
			'extensionId',
			array 
			(
				'isRequired' => true,
				'type' => TYPE_STRING_LONG
			)
		);

		$this->createProperty
		(
			'hours',
			array
			(
				'isRequired' => true,
				'type' => TYPE_STRING_LONG
			)
		);

		$this->createProperty
		(
			'isRepeatable',
			array
			(
				'isRequired' => true,
				'type' => TYPE_BOOLEAN
			)
		);

		$this->createProperty
		(
			'jobInstances',
			array
			(
				'className' => 'CJobInstance',
				'extensionId' => EXTENSION_LOOP,
				'isComposition' => true,
				'isDirect' => true
			)
		);

		$this->createProperty
		(
			'minutes',
			array
			(
				'isRequired' => true,
				'type' => TYPE_STRING_LONG
			)
		);

		$this->createProperty
		(
			'months',
			array
			(
				'isRequired' => true,
				'type' => TYPE_STRING_LONG
			)
		);		
		
		$this->createProperty
		(
			'name',
			array 
			(
				'isName' => true,
				'isRequired' => true,
				'type' => TYPE_STRING_LONG
			)
		);

		$this->createProperty
		(
			'timeOfHolding',
			array 
			(
				'type' => TYPE_TIMESTAMP
			)
		);

		$this->createProperty
		(
			'timeOfRelease',
			array 
			(
				'type' => TYPE_TIMESTAMP
			)
		);

		$this->createProperty
		(
			'timeOfRelease',
			array 
			(
				'type' => TYPE_TIMESTAMP
			)
		);
	}


	/**
	 *
	 */
	
	public function hold()
	{
		$this->setTimeOfHolding(time());
		$this->setTimeOfRelease(null);
		$this->save();
	}


	/**
	 *
	 */
	
	public function isReleased()
	{
		$timeOfRelease = $this->getTimeOfRelease();
		$timeOfHolding = $this->getTimeOfHolding();

		if 
		(
			empty($timeOfRelease) == false &&
			$timeOfRelease <= time() &&
			empty($timeOfHolding) == true
		)
		{
			return true;
		}
		else
		{
			return false;
		}
			
	}

	
	/**
	 *
	 */
	
	public function release()
	{
		$this->setTimeOfRelease(time());
		$this->setTimeOfHolding(null);
		$this->save();
	}


	/**
	 *
	 */

	public function setArguments($arguments)
	{
		if 	($arguments == ' ')
		{
			$this->setArguments(null);
		}

		parent::setPropertyValue('arguments', $arguments);
	}


	/**
	 *
	 */

	public function setDays($days)
	{
		if 	($days == ' ')
		{
			$this->setDays(null);
		}

		parent::setPropertyValue('days', $days);	
	}


	/**
	 *
	 */

	public function setDaysOfWeek($days)
	{
		if 	($days == ' ')
		{
			$this->setDaysOfWeek(null);
		}

		parent::setPropertyValue('daysOfWeek', $days);	
	}


	/**
	 *
	 */

	public function setHours($hours)
	{
		if 	($hours == ' ')
		{
			$this->setHours(null);
		}

		parent::setPropertyValue('hours', $hours);	
	}


	/**
	 *
	 */

	public function setMinutes($minutes)
	{
		if 	($minutes == ' ')
		{
			$this->setDaysOfWeek(null);
		}

		parent::setPropertyValue('minutes', $minutes);
	}


	/**
	 *
	 */

	public function setMonths($months)
	{
		if 	($months == ' ')
		{
			$this->setMonths(null);
		}

		parent::setPropertyValue('months', $months);
	}

	
	/**
	 *
	 */

	public function run()
	{
		// Dependencies

		require_once(PATH_LOOP . 'Class/CApiFactory.php');


		// Create object

		$apiFactory = new \Loop\CApiFactory();


		// Get the arguments

		$arguments = json_decode($this->getArguments());


		// Prepare variable

		if (is_null($arguments) === false)
		{

			// For each argument

			foreach ($arguments as $name => $value)
			{
				// Is it an array ?

				if (is_array($value) === true)
				{
					// Set the value

					$value = json_encode($value);
				}


				// Construct the command arguments

				v($name, $value);
			}
		}


		// Construct command

		try
		{
			$result['result'] = $apiFactory->run($this->getClassName(), $this->getApiName(), $this->getExtensionId());
		}
		catch(\Exception $e)
		{
			$result['exceptions'] = $e;
		}


		// If the job is not repeatable we hold it after the first execution

		if ($this->getIsRepeatable() == false)
		{
			$this->hold();
		}


		
		return $result;
	}
}

?>
